/**
 * 
 */
package com.ksv.lop;

import java.util.ArrayList;

import com.ksv.qlsv.R;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class KhoaAdapter extends BaseAdapter {

	private Context mContext;
	public ArrayList<GetData> mArrGetData;
	public static String mID;
	public static String mName;

	public KhoaAdapter(Context context, Cursor cur) {
		// TODO Auto-generated constructor stub
		mContext = context;
		mArrGetData = getDataFromCursor(cur);
	}
	
	private ArrayList<GetData> getDataFromCursor(Cursor c){
		ArrayList<GetData> mang = new ArrayList<GetData>();
		c.moveToFirst();
		while (c.getPosition() >= 0) {
			GetData data = new GetData();
			data.setID(c.getInt(c.getColumnIndex(mID)));
			data.setName(c.getString(c.getColumnIndex(mName)));
			mang.add(data);
			if(c.getPosition() >= c.getCount() - 1){
				data = new GetData();
				data.setID(0);
				data.setName(mContext.getResources().getString(R.string.chontatca));
				mang.add(data);
				break;
			}
			c.moveToNext();
		}
		return mang;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mArrGetData.size();
	}

	@Override
	public GetData getItem(int position) {
		// TODO Auto-generated method stub
		return mArrGetData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return mArrGetData.get(position).getID();
	}

	@Override
	public View getView(int position, View view, ViewGroup viewGroup) {
		// TODO Auto-generated method stub
		LayoutInflater layoutInflater = LayoutInflater.from(mContext);
		LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(R.layout.listrow_khoa, null);
		TextView txt = (TextView) linearLayout.findViewById(R.id.txt_name);
		txt.setText(mArrGetData.get(position).getName());		
		return linearLayout;
	}
}
